<template>
	<view class="app">
		<!-- <u-swipe-action :show="item.show" :index="index" 
					v-for="(item, index) in list" :key="item.id" 
					@click="click" @open="open" @content-click="chooseAddress(index)"
					:options="options"
				>
				
				<view class="item" >
					<view class="add_name">{{ item.name }}</view>
					<view class="add_value">{{ item.address.substring(0,37) }}</view>
					<view class="add_value">{{ item.address.substring(37) }}</view>
				</view>
			
		</u-swipe-action> -->
				
		
		<view v-for="(item, index) in list" :key="index" >
			<view class="item" >
				<view class="item_text" @click="chooseAddress(index)">
					<view class="add_name">{{ item.name }}</view>
					<view class="add_value">{{ item.address }}</view>
				</view>
				<image src="/static/icon/delete2.png" class="delete_icon" @click="deleteClick(index)"></image>
			</view>
		</view>
		<!-- 缺少时 -->
		<view v-if="loaded && list.length === 0">
			<view class="no_list">当前还没有LHT地址，赶快添加一个吧~</view>
		</view>
		<!-- <view class="tip">注：左滑删除地址</view> -->
		
		<view class="add_btn" @click="navTo('/pages/purse/lhtAddress/addAddress/addAddress')">添加地址</view>
		
		<mix-modal ref="mixModal" title="提示" text="确定要删除该地址吗" @onConfirm="deleteAddr"></mix-modal>
		<mix-loading v-if="isLoading" :type="list.length > 0 ? 1 : 2"></mix-loading>
		<!-- 缺省 -->
		<!-- <mix-empty v-else-if="loaded && list.length === 0" type="address" backgroundColor="#fff"></mix-empty> -->
	</view>
</template>

<style>
	/* 相对路径 */
	@import url('./lhtAddress.css');
</style>

<script>
	let that = this;
	export default {
		data() {
			return {
				list: [],
				options: [
						{
							text: '删除',
							style: {
								backgroundColor: '#dd524d'
							}
						}
					]
			}
		},
		onLoad(options) {
			this.isChoose = !!options.choose;
			this.choosedId = options.id;
			this.loadData();
		},
		methods: {
			async loadData(){
				const res = await this.$request('lhtAddress', 'list', {}, {
					showLoading: !this.loaded
				});
				this.list = res.data;
			},
			//选择地址
			chooseAddress(index){
				console.log(index);
				console.log(JSON.stringify(this.list[index]));
				this.$util.prePage().setAddress(this.list[index]);
				uni.navigateBack();
			},
			/**
			 * 删除地址
			 * 注意 删除成功已选择的地址需要清空
			 */
			async deleteAddr(curIndex){
				console.log("addressId="+ JSON.stringify(this.list[curIndex]));
				const res = await this.$request('lhtAddress', 'remove', {
					addressId: this.list[curIndex]._id
				}, {
					showLoading: true
				})
				this.$util.msg(res.msg);
				if(res.status === 1){
					this.list.splice(this.curIndex, 1);
					this.isChoose && this.choosedId && this.$util.prePage().setAddress({});
				}
			},
		
			clearLoop(e) {
				clearTimeout(this.Loop);
			},
			
			deleteClick(index) {
				 this.curIndex = index;
				 that = this;
				 uni.showModal({
				     title: '提示',
				     content: '是否删除此地址？',
				     success: function (res) {
				         if (res.confirm) {
				             console.log('用户点击确定');
							 that.deleteAddr(index);
				         }
				     }
				 });
			},
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			open(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				this.list[index].show = true;
				this.list.map((val, idx) => {
					if(index != idx) this.list[idx].show = false;
				})
			}
		}
	}
</script>
